
package com.idealighter.game.games.sirenniuniu.handler;

import com.idealighter.game.core.annotation.ResMsgHandler;
import com.idealighter.game.dictionary.domain.SirenniuniuRoomDomain;
import com.idealighter.game.gamehall.dto.MemInfo;
import com.idealighter.game.gamehall.dto.SeatInfo;
import com.idealighter.game.gamehall.dto.TableInfo;
import com.idealighter.game.games.sirenniuniu.SiRenNiuNiuPlayer;
import com.idealighter.game.games.sirenniuniu.message.ReqBetMsg;
import com.idealighter.game.games.sirenniuniu.message.ResCallDealerOverMsg;
import com.idealighter.game.message.ModuleMsgIdConstant;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.robot.core.Player;
import com.idealighter.game.robot.handler.ResMessageHandler;
import com.idealighter.utils.check.EmptyUtil;
import com.idealighter.utils.code.RandCodeUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@ResMsgHandler(ModuleMsgIdConstant.SiRenNiuNiu.RES_CALL_DEALER_OVER)
public class ResCallDealerOverHandler implements ResMessageHandler {

  private static final Logger LOG = LoggerFactory.getLogger(ResCallDealerOverHandler.class);

  @Override
  public void action(Player player, ResMessage message) {
    ResCallDealerOverMsg msg = (ResCallDealerOverMsg) message;
    // 庄家id
    long dealerId = msg.getDealer();
    MemInfo member = player.member(player.playerId);

    if (dealerId != member.getPlayerId()) { // 机器人下注
      // 随机5到8秒下注
      player.schedule(() -> {
        List<Long> betOptions = abledBetChips(player, dealerId);
        if (EmptyUtil.listIsNotEmpty(betOptions)) {
          long chips = RandCodeUtil.randomList(betOptions);
          bet(player, chips);
        } else {
          player.delayLogout();
        }
      }, RandCodeUtil.random(5, 8), TimeUnit.SECONDS);
    }
  }

  /**
   * 下注.
   * 
   * @param player .
   * @param chip .
   */
  public void bet(Player player, Long chip) {
    LOG.info("玩家[{}][{}]在房间[{}]中下注[{}]", player.playerId, player.playerInfo.getUserName(),
        player.roomId, chip);
    ReqBetMsg msg = new ReqBetMsg();
    msg.setChip(chip);
    player.sendMsg(msg);
  }

  /**
   * 下注列表.
   * 
   * @param player .
   * @param dealerId . 庄家id
   * @return .
   */
  private List<Long> abledBetChips(Player player, long dealerId) {
    TableInfo table = player.table(player.playerId);
    SirenniuniuRoomDomain roomDom = ((SiRenNiuNiuPlayer) player).roomDomain;

    // 闲家合计筹码
    long playesChips = 0;
    for (SeatInfo s : table.getSeats()) {
      long playerId = s.getPlayerId();
      if (playerId > 0 && playerId != dealerId) {
        MemInfo m = player.member(playerId);
        playesChips += m.getChips();
      }
    }

    MemInfo member = player.member(player.playerId);
    MemInfo dealer = player.member(dealerId);
    // 庄家用于计算下注的分配给闲家的比例筹码
    long ratioChips = dealer.getChips() * member.getChips() / playesChips;
    long minChips = member.getChips() > ratioChips ? ratioChips : member.getChips();

    long baseBet = roomDom.getBaseBet();
    List<Integer> betOptions = roomDom.getBetOptions();

    List<Long> result = new ArrayList<Long>();
    for (int i = betOptions.size() - 1; i >= 0; i--) {
      Integer option = betOptions.get(i);
      if (minChips >= baseBet * option) {
        for (int j = 0; j <= i; j++) {
          result.add(baseBet * betOptions.get(j));
        }
        break;
      }
    }
    return result;
  }
}
